using System;
using System.IO;
using System.Web.UI;

namespace ScottCateAjax2008.Utilities
{
    public partial class ShowServerCode : Page
    {
        protected void Page_Load ( object sender, EventArgs e )
        {
            if (!string.IsNullOrEmpty( Request.QueryString["Res"] ))
            {
                string res = Request.QueryString["Res"];
                string path = Server.MapPath(res);
                if (File.Exists(path))
                {
                    string contents = File.ReadAllText(path);
                    string textarea = "<textarea name=\"code\" class=\"{0}\">{2}{2}{1}{2}{2}{2}</textarea>";

                    switch (Path.GetExtension(path).ToLower())
                    {
                        case ".cs":
                            contents = string.Format(textarea, "csharp", contents, Environment.NewLine);
                            break;
                        case ".js":
                            contents = string.Format(textarea, "js", contents, Environment.NewLine);
                            break;
                        default:
                            contents = string.Format(textarea, "xml", contents, Environment.NewLine);
                            break;
                    }

                    SourceCodePlaceHolder.Controls.Add(new LiteralControl(contents));

                    string aspxPath = path.Replace("aspx.cs", "aspx").Replace("aspx.atlas", "aspx");
                    if (aspxPath.ToLower().EndsWith(".aspx") && File.Exists(aspxPath))
                    {
                        AspxFileLink.NavigateUrl = res.Replace("aspx.cs", "aspx").Replace("aspx.atlas", "aspx");
                        AspxFileLink.Text = Path.GetFileNameWithoutExtension(AspxFileLink.NavigateUrl);
                    }
                }
            }
        }
    }
}